package service

import (
	"fmt"
	"go.uber.org/zap"
	"iot-base/common/cache"
	"iot-base/common/ctrltype"
	"iot-base/common/logger"
	"iot-base/common/notify"
	"regexp"
	"strconv"
	"strings"
	"time"

	"errors"
)

type NoxLogic struct {
}

func NewNoxLogic() *NoxLogic {
	return &NoxLogic{}
}

// Nox 氮氧化物 5300.
func (this *NoxLogic) Nox(id string, thing *cache.ThingInfo, v interface{}, round int64) (interface{}, error) {
	//ip := thing.GetThingParam("ip", "")
	//port := thing.GetThingParam("port", "")
	//noxStr, err := SynHub.SendStrData(ip+":"+port, "noxchr$")
	b, err := RequestData(thing, false, []byte("noxchr$"))
	noxStr := string(b)

	if strings.Contains(noxStr, "error") {
		logger.Log.Error("Nox service.SynHub.SendStrData err:" + noxStr)
		fmt.Println("noxchr$", noxStr)
		return nil, err
	}
	logger.Log.Info("nox noxchr$ info:=" + noxStr)
	fmt.Println("noxchr$ info:=", noxStr, err)
	if err != nil {
		logger.Log.Error("Nox service.SynHub.SendStrData err:", zap.Error(err))
		return nil, err
	}
	arr := strings.Split(noxStr, "\n")
	var status float64 = 1
	now := time.Now().Unix()
	for _, v := range arr {
		temp := strings.Split(v, "=")
		name := temp[0]
		logger.Log.Info(name + "=" + temp[1])
		switch name[0:1] {
		case "*":
			status = 2
			name = name[1:]
		case "#":
			status = 3
			name = name[1:]
		}
		reg := regexp.MustCompile("[\\-]?[\\d]+\\.?[\\d]+")
		data := reg.Find([]byte(temp[1]))
		value, _ := strconv.ParseFloat(string(data), 64)
		fmt.Println("调试:", strings.ToLower(noxStr), value)
		notify.RawData(thing.Thing.UID, strings.ToLower(name), "", 0, value, now, now)
		notify.RawData(thing.Thing.UID, strings.ToLower(name)+"ValueStatus", "", 0, fmt.Sprintf("%s%v", name[0:1], value), now, now)
	}
	notify.RawData(thing.Thing.UID, "status", "", 0, status, now, now)

	// 详细状态.
	//noxtwcStr, err := SynHub.SendStrData(ip+":"+port, "noxtwc$")
	b, err = RequestData(thing, false, []byte("noxtwc$"))
	noxtwcStr := string(b)
	if strings.Contains(noxtwcStr, "error") {
		logger.Log.Error("Nox noxtwc$ service.SynHub.SendStrData err:" + noxtwcStr)
		fmt.Println("noxtwcStr", noxtwcStr)
		return nil, err
	}
	logger.Log.Info("nox noxtwcStr info:=" + noxtwcStr)
	fmt.Println("noxtwcStr info:=", noxtwcStr, err)
	if err != nil {
		logger.Log.Error("Nox noxtwcStr service.SynHub.SendStrData err:", zap.Error(err))
		return nil, err
	}
	noxtwcStr = noxtwcStr[:len(noxtwcStr)-1]
	noxtwcArr := strings.Split(noxtwcStr, ",")

	// 上报数据.
	ReportDataFloat64(thing.Thing.UID, noxtwcArr, NoxField)
	return nil, nil
}

var NoxField = map[int]string{
	1:  "pmt_fileter_nov",
	2:  "pmt_fileter_noxv",
	3:  "power",
	4:  "press",
	5:  "flow",
	6:  "temp",
	7:  "boxtemp",
	8:  "mulu_temp",
	9:  "canbi_v",
	10: "cell_press",
	11: "sample_flow",
	12: "pmt_hvdis",
}

func (this *NoxLogic) NoxSv(pCmd *ctrltype.CtrlCmd, tingInfo *cache.ThingInfo) (map[string]interface{}, error) {
	//ip := tingInfo.GetThingParam("ip", "")
	//port := tingInfo.GetThingParam("port", "")
	var requestMap map[string]interface{}
	requestMap = make(map[string]interface{})
	switch pCmd.ID {
	case "nzeros":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "nzeros$")
		b, err := RequestData(tingInfo, true, []byte("nzeros$"))
		readbuffer := string(b)

		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "nzeroe":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "nzeroe$")
		b, err := RequestData(tingInfo, true, []byte("nzeroe$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "nzeroq":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "nzeroq$")
		b, err := RequestData(tingInfo, true, []byte("nzeroq$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "nspans":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "nspans "+concentration+"$")
		b, err := RequestData(tingInfo, true, []byte("nspans "+concentration+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "nspane":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "nspane "+concentration+"$")
		b, err := RequestData(tingInfo, true, []byte("nspane "+concentration+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "nspanq":
		concentration, ok := pCmd.Params["concentration"].(string)
		if !ok {
			return nil, errors.New("concentration type is err")
		}
		if len(concentration) < 3 {
			for i := 0; i < 4-len(concentration); i++ {
				concentration = "0" + concentration
			}
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "nspanq "+concentration+"$")
		b, err := RequestData(tingInfo, true, []byte("nspanq "+concentration+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "ok") {
			requestMap["msg"] = "ok"
		} else {
			requestMap["msg"] = "fail:" + readbuffer
			return requestMap, errors.New("fail:" + readbuffer)
		}
		return requestMap, nil
	case "status":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "noxchr$")
		b, err := RequestData(tingInfo, true, []byte("noxchr$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("Nox service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("noxchr$", readbuffer)
			return nil, err
		}
		logger.Log.Info("nox noxchr$ info:=" + readbuffer)
		fmt.Println("noxchr$ info:=", readbuffer, err)
		if err != nil {
			logger.Log.Error("Nox service.SynHub.SendStrData err:", zap.Error(err))
			return nil, err
		}
		switch readbuffer[:1] {
		case "*":
			requestMap["msg"] = "2"
		case "#":
			requestMap["msg"] = "3"
		default:
			requestMap["msg"] = "1"
		}
		return requestMap, nil
	case "value":
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "noxchr$")
		b, err := RequestData(tingInfo, true, []byte("noxchr$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("Nox service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("noxchr$", readbuffer)
			return nil, err
		}
		logger.Log.Info("nox noxchr$ info:=" + readbuffer)
		fmt.Println("noxchr$ info:=", readbuffer, err)
		if err != nil {
			logger.Log.Error("Nox service.SynHub.SendStrData err:", zap.Error(err))
			return nil, err
		}
		data := strings.Split(readbuffer, "=")
		if len(data) != 2 {
			return nil, errors.New("return data is err:" + readbuffer)
		}
		data[1] = strings.Replace(data[1], "$", "", -1)
		if data[1][0] == '*' || data[1][0] == '#' {
			requestMap["value"] = data[1][1:]
		} else {
			requestMap["value"] = data[1]
		}
		return requestMap, nil
	case "his":
		start, ok := pCmd.Params["start"].(string)
		if !ok {
			return nil, errors.New("start type is err")
		}
		end, ok := pCmd.Params["end"].(string)
		if !ok {
			return nil, errors.New("end type is err")
		}
		//readbuffer, err := SynHub.SendStrData(ip+":"+port, "noxhis"+start+" "+end+"$")
		b, err := RequestData(tingInfo, true, []byte("noxhis"+start+" "+end+"$"))
		readbuffer := string(b)
		if err != nil {
			return nil, errors.New("read err:" + err.Error())
		}
		if strings.Contains(readbuffer, "error") {
			logger.Log.Error("nox service.SynHub.SendStrData err:" + readbuffer)
			fmt.Println("noxhis", readbuffer)
			return nil, err
		}
		requestMap["result"] = readbuffer
		return requestMap, nil
	default:
		return nil, errors.New("no service")
	}

}
